﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class Main_Inventory_PurchaseReturnEdit : System.Web.UI.Page
{
    Datautility du = new Datautility();
    string strsql = "";
    int controlCounterBillLevel = 0;
    #region Global Variable for Tax At Bill Level

    List<string> LstCountBillLevel; //for store count record
    List<string> LstItemBillLevel; // record Item
    List<string> LstItemIdBillLevel;// hidden field to maintaining item
    List<string> LstUnitBillLevel;// record unit
    List<string> LstUnitIdBillLevel; // hidden field maintaining unit
    List<string> LstQtyBillLevel; // record Qty
    List<string> LstRateBillLevel; // record Rate
    List<string> LstButtonBillLevel; // btn
    List<string> LstLocationBillLevel; // ??
    List<string> LstTaxRateBillLevel;// record tax rate
    List<string> LstTaxAmtBillLevel; // record tax amt
    List<string> LstGrossAmtBillLevel; // record gross amt
    List<string> LstTaxTypeBillLevel; // record tax type
    List<string> LstTaxAmountBillLevel; // record tax amt
    List<string> LstTotalAmountBillLevel; // record totalamt
    #endregion    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BillFormat();
            Supplier();
            TaxGroup();
            IsTranFormatTaxAtItemLevel(ddlFormat.SelectedValue);
        }       
    }

    protected void BillFormat()
    {
        SqlParameter[] parm = {
                                      new SqlParameter("@TranType","6")
                              };
        DataTable dtformat = du.getdatausingprocedure("USP_IVR_GetTransactionFormat", parm);
        if (dtformat.Rows.Count > 0)
        {
            ddlFormat.DataSource = dtformat;
            ddlFormat.DataTextField = "FormatName";
            ddlFormat.DataValueField = "Tfoid";
            ddlFormat.DataBind();
            ddlFormat.SelectedIndex = 0;
            VoucherNo();
        }
    }

    protected void VoucherNo()
    {
        if (ddlFormat.SelectedValue != "0")
        {
            SqlParameter[] parm = {
                                     new SqlParameter("@TFOID",ddlFormat.SelectedValue),
                                     new SqlParameter("@FINYEAR",Session["FINYEAR"].ToString())
                                  };
            DataTable dtvoucher = du.getdatausingprocedure("USP_IVR_GetMaxVoucherNumber", parm);
            if (dtvoucher.Rows.Count > 0)
            {
                txtPrefix.Text = dtvoucher.Rows[0]["NUMPREFIX"].ToString();
                txtVoucher.Text = dtvoucher.Rows[0]["NUMSTART"].ToString();
                txtSuffix.Text = dtvoucher.Rows[0]["NUMSUFFIX"].ToString();
            }
        }
    }

    protected void ddlFormat_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            Session["DTPurchaseReturn"] = null;
            VoucherNo();
            IsTranFormatTaxAtItemLevel(ddlFormat.SelectedValue);
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
    }

    protected void Supplier()
    {
        SqlParameter[] parm = {
                                  new SqlParameter("@TRANTYPE", "4")
                              };
        DataTable dtsupplier = du.getdatausingprocedure("USP_IVR_GetLedgerForSalesPurchaseReturn", parm);
        if (dtsupplier.Rows.Count > 0)
        {
            ddlSupplier.DataSource = dtsupplier;
            ddlSupplier.DataTextField = "LName";
            ddlSupplier.DataValueField = "LID";
            ddlSupplier.DataBind();
        }
    }

    protected void TaxGroup()
    {
        DataTable dttax = du.getdatausingprocedure("USP_ACC_GetPurchaseMainClass");
        if (dttax.Rows.Count > 0)
        {
            ddlTaxGroup.DataSource = dttax;
            ddlTaxGroup.DataTextField = "MAINCLASSNAME";
            ddlTaxGroup.DataValueField = "MAINCLASSID";
            ddlTaxGroup.DataBind();
            ddlTaxGroup.Items.Insert(0, new ListItem("", "0"));            
        }
    }
    protected void IsTranFormatTaxAtItemLevel(string strTranFormat)//Check Transaction format Tax at item Level OR Not 
    {
        if (strTranFormat.ToString() != string.Empty)
        {
            strsql = "SELECT TAXATITEM FROM IVR_TRANFORMAT WHERE TFOID='" + strTranFormat.ToString() + "'";
            DataTable DTFormat = du.getdata(strsql);
            if (DTFormat.Rows.Count > 0)
            {
                ///if True then tax at item level
                ///else Tax at Bill Level
                if (DTFormat.Rows[0]["TAXATITEM"].ToString() == "True")
                {
                    hidTranFormatType.Value = "True";
                    //ddlVatBillLevel.Visible = false;
                    //ddlVatBillLevel.Enabled = false;
                    //panelSaleBillLevel.Visible = false;
                    //ddlSaleLedger.Visible = false;
                    //ddlSaleLedger.Enabled = false;
                    //ddlSaleTaxLedger.Visible = false;
                    //ddlSaleTaxLedger.Enabled = false;
                    trBillLevel.Visible = false;
                    trItemLevel.Visible = true;
                }
                else if (DTFormat.Rows[0]["TAXATITEM"].ToString() == "False")
                {
                    hidTranFormatType.Value = "False";
                    //ddlVatBillLevel.Visible = true;
                    //ddlVatBillLevel.Enabled = true;
                    //panelSaleBillLevel.Visible = true;
                    //ddlSaleLedger.Visible = true;
                    //ddlSaleLedger.Enabled = true;
                    //ddlSaleTaxLedger.Visible = true;
                    //ddlSaleTaxLedger.Enabled = true;
                    trBillLevel.Visible = true;
                    trItemLevel.Visible = false;
                }
            }
        }
    }       

    protected void AddMoreBillLevel()//Add more row for tax at bill level
    {
        if (hidTranFormatType.Value == "True")
        {
            trBillLevel.Visible = false;
            trItemLevel.Visible = true;
        }
        IsTranFormatTaxAtItemLevel(ddlFormat.SelectedValue);
    }
}

        //    BindDdlTaxType();
        //   

        //    if (hidTranFormatType.Value.ToString() == "True")
        //    {
        //        trItemLevel.Visible = true;
        //        trBillLevel.Visible = false;
        //    }
        //    else if (hidTranFormatType.Value.ToString() == "False")
        //    {
        //        trItemLevel.Visible = false;
        //        trBillLevel.Visible = true;
        //    }
        //}